/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.eduhzy.ttw.teacher.app;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.multidex.MultiDex;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.Utils;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.utils.WXPay;
import com.eduhzy.ttw.teacher.BuildConfig;
import com.jess.arms.base.delegate.AppLifecycles;
import com.jess.arms.utils.ArmsUtils;
import com.mob.MobSDK;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import cn.jpush.android.api.JPushInterface;
import io.realm.Realm;
import me.jessyan.retrofiturlmanager.RetrofitUrlManager;

import static com.eduhzy.ttw.commonsdk.http.CommonApi.APP_UPLOAD_DOMAIN;
import static com.eduhzy.ttw.commonsdk.http.CommonApi.APP_UPLOAD_DOMAIN_NAME;
import static com.eduhzy.ttw.teacher.mvp.model.api.Api.APP_DOMAIN;
import static com.eduhzy.ttw.teacher.mvp.model.api.Api.APP_DOMAIN_NAME;

/**
 * ================================================
 * 展示 {@link AppLifecycles} 的用法
 * <p>
 * Created by JessYan on 04/09/2017 17:12
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class AppLifecyclesImpl implements AppLifecycles {

    @Override
    public void attachBaseContext(@NonNull Context base) {
        MultiDex.install(base);  //这里比 onCreate 先执行,常用于 MultiDex 初始化,插件化框架的初始化
    }

    @Override
    public void onCreate(@NonNull Application application) {
        if (LeakCanary.isInAnalyzerProcess(application)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        /**
         * 注意: 即使您已经在AndroidManifest.xml中配置过appkey和channel值，也需要在App代码中调
         * 用初始化接口（如需要使用AndroidManifest.xml中配置好的appkey和channel值，
         * UMConfigure.init调用中appkey和channel参数请置为null）。
         */
        UMConfigure.init(application, Constants.UMENG_APPKEY, "Umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.LEGACY_AUTO);
        // 选用AUTO页面采集模式
        UMConfigure.setLogEnabled(BuildConfig.DEBUG);
        Utils.init(application);
        String deviceId = DeviceUtils.getModel() + "-" + DeviceUtils.getManufacturer() + "-ANDROID:" + DeviceUtils.getSDKVersionName() + "-V" + AppUtils.getAppVersionName();
        SPUtils.getInstance().put(Constants.DEVICEID, deviceId, SPUtils.getInstance().contains(Constants.DEVICEID));
        MobSDK.init(application);
        Realm.init(application);

        WXPay.init(application, Constants.WX_APP_ID);

        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.init(application);

        CrashReport.initCrashReport(application, "ce8375f721", BuildConfig.DEBUG);
        RetrofitUrlManager.getInstance().putDomain(APP_DOMAIN_NAME, APP_DOMAIN);
        RetrofitUrlManager.getInstance().putDomain(APP_UPLOAD_DOMAIN_NAME, APP_UPLOAD_DOMAIN);
        //leakCanary内存泄露检查
        ArmsUtils.obtainAppComponentFromContext(application).extras().put(RefWatcher.class.getName(), BuildConfig.USE_CANARY ? LeakCanary.install(application) : RefWatcher.DISABLED);

    }

    @Override
    public void onTerminate(@NonNull Application application) {
        ARouter.getInstance().destroy();
    }
}
